﻿CREATE PROCEDURE [dbo].[spDeleteGame]
	@GameID int,
	@UpdateUser varchar(100)
as
begin
begin tran	


	declare @UpdateDate Datetime 
	set @UpdateDate = getdate()

	update Games
	set Deleted = 1,
		UpdateDate = @UpdateDate,
		UpdateUser = @UpdateUser
	where Id = @GameID
	
	update gt
	set gt.Deleted = 1,
		UpdateDate = @UpdateDate,
		UpdateUser = @UpdateUser
	from GameTeams gt
		inner join Games g on g.BlackTeamId = gt.Id or g.WhiteTeamId = gt.Id
	where 
		g.Id = @GameID
		
	update PlayerHistories
	set Deleted = 1,
		UpdateDate = @UpdateDate,
		UpdateUser = @UpdateUser
	from PlayerHistories, Games g
	where convert(varchar(19),HIstoryInsertDate,20) = convert(varchar(19),g.insertdate,20)
		and g.Id = @GameID
		
	exec('DISABLE TRIGGER trPlayersUpdate ON Players')
		
	--Black Attacker
	update p
	set p.AccountPoints = p.AccountPoints - gt.AttackerEarnedPoints,
		UpdateDate = @UpdateDate,
		UpdateUser = @UpdateUser	
	from Players p
		inner join GameTeams gt on gt.AttackerId = p.id
		inner join Games g on g.BlackTeamId = gt.Id
	where g.Id = @GameID

	update ph
	set ph.AccountPoints = ph.AccountPoints - gt.AttackerEarnedPoints,
		ph.UpdateDate = @UpdateDate,
		ph.UpdateUser = @UpdateUser	
	from PlayerHistories ph
		inner join GameTeams gt on gt.AttackerId = ph.Playerid
		inner join Games g on g.BlackTeamId = gt.Id
	where convert(varchar(19),ph.HIstoryInsertDate,20) <> convert(varchar(19),g.insertdate,20)
		and ph.HIstoryInsertDate > g.insertdate
		and g.Id = @GameID

	--Black Defender
	update p
	set p.AccountPoints = p.AccountPoints - gt.DefenderEarnedPoints,
		UpdateDate = @UpdateDate,
		UpdateUser = @UpdateUser
	from Players p
		inner join GameTeams gt on gt.DefenderId = p.id
		inner join Games g on g.BlackTeamId = gt.Id
	where g.Id = @GameID
	
	update ph
	set ph.AccountPoints = ph.AccountPoints - gt.DefenderEarnedPoints,
		ph.UpdateDate = @UpdateDate,
		ph.UpdateUser = @UpdateUser	
	from PlayerHistories ph
		inner join GameTeams gt on gt.DefenderId = ph.Playerid
		inner join Games g on g.BlackTeamId = gt.Id
	where convert(varchar(19),ph.HIstoryInsertDate,20) <> convert(varchar(19),g.insertdate,20)
		and ph.HIstoryInsertDate > g.insertdate
		and g.Id = @GameID		
		
	--White Attacker
	update p
	set p.AccountPoints = p.AccountPoints - gt.AttackerEarnedPoints,
		UpdateDate = @UpdateDate,
		UpdateUser = @UpdateUser
	from Players p
		inner join GameTeams gt on gt.AttackerId = p.id
		inner join Games g on g.WhiteTeamId = gt.Id
	where g.Id = @GameID

	update ph
	set ph.AccountPoints = ph.AccountPoints - gt.AttackerEarnedPoints,
		ph.UpdateDate = @UpdateDate,
		ph.UpdateUser = @UpdateUser	
	from PlayerHistories ph
		inner join GameTeams gt on gt.AttackerId = ph.Playerid
		inner join Games g on g.WhiteTeamId = gt.Id
	where convert(varchar(19),ph.HIstoryInsertDate,20) <> convert(varchar(19),g.insertdate,20)
		and ph.HIstoryInsertDate > g.insertdate
		and g.Id = @GameID
	
	--White Deffender	
	update p
	set p.AccountPoints = p.AccountPoints - gt.DefenderEarnedPoints,
		UpdateDate = @UpdateDate,
		UpdateUser = @UpdateUser
	from Players p
		inner join GameTeams gt on gt.DefenderId = p.id
		inner join Games g on g.WhiteTeamId = gt.Id
	where g.Id = @GameID

	update ph
	set ph.AccountPoints = ph.AccountPoints - gt.DefenderEarnedPoints,
		ph.UpdateDate = @UpdateDate,
		ph.UpdateUser = @UpdateUser	
	from PlayerHistories ph
		inner join GameTeams gt on gt.DefenderId = ph.Playerid
		inner join Games g on g.WhiteTeamId = gt.Id
	where convert(varchar(19),ph.HIstoryInsertDate,20) <> convert(varchar(19),g.insertdate,20)
		and ph.HIstoryInsertDate > g.insertdate
		and g.Id = @GameID
		
	exec('ENABLE TRIGGER trPlayersUpdate ON Players')
	
commit tran	

end
